package com.sky.d_thread.sync;

/**
 * 实例方法级别的锁
 * @Author:hjt
 */
public class AccouttingSyncClass implements Runnable {
	static AccouttingSyncClass instance = new AccouttingSyncClass();
	static int i = 0;

	//静态方法上加锁，是属于类级别的锁
	public static synchronized void increase(){
		i ++;
	}
	@Override
	public void run() {
		for (int j = 0;j < 1000000; j ++){
			increase();
		}
		
	}
	
	public static void main(String[] args) throws InterruptedException {
		//预期两次线程都正常执行，数据是两次之和
		
		//两个不一样的实例，加锁的对象不一样，数据不能达到预期
		Thread t1 = new Thread(new AccouttingSyncClass());
		Thread t2 = new Thread(new AccouttingSyncClass());
		
		t1.start();
		t2.start();
		
		//join 等待线程执行完毕，打印两次线程一起的结果
		t1.join();
		t2.join();
		
		System.out.println("执行完毕,i=" + i);
	}
}
